/*
 * @Description: 系统通知   SystemNotification
 * @version: 
 * @Company: 
 * @Author: ZhengZhuang
 * @Date: 2019-08-26 17:28:08
 * @LastAuthor   : zhengzhuang
 * @lastTime     : 2020-01-04 09:29:33
 */
import 'package:flutter/material.dart';
import 'package:async/async.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:C20Driver/widget/no_data_widget.dart';
import 'package:provider/provider.dart';
import 'package:C20Driver/model/get_message_list.dart';
import 'package:C20Driver/provider/message_center_provider.dart';
import 'package:C20Driver/routers/navigator_util.dart';
import 'package:C20Driver/widget/spinkit.dart';

class SystemNotification extends StatefulWidget {
  @override
  _SystemNotificationState createState() => _SystemNotificationState();
}

class _SystemNotificationState extends State<SystemNotification>
    with AutomaticKeepAliveClientMixin {
  final AsyncMemoizer _memoizer = AsyncMemoizer();

  // 获取消息列表
  Future _getMessageList() async {
    return _memoizer.runOnce(() async {
      return await Provider.of<MessageCenterProvider>(context).getMessageList();
    });
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    MessageCenterProvider provider =
        Provider.of<MessageCenterProvider>(context);
    return FutureBuilder(
      future: _getMessageList(),
      builder: (BuildContext context, AsyncSnapshot snapshot) {
        if (snapshot.hasData) {
          if (provider.notification.length == 0) return NoDataWidget();
          return Container(
            child: EasyRefresh(
              header: ClassicalHeader(
                bgColor: Colors.white,
                textColor: Colors.black,
                noMoreText: '',
                refreshText: '下拉刷新',
                refreshReadyText: '松开刷新',
                refreshingText: '正在刷新中',
                refreshedText: '刷新完成',
                showInfo: false,
              ),
              footer: ClassicalFooter(
                bgColor: Colors.white,
                textColor: Colors.black,
                noMoreText: '',
                loadText: '下拉刷新',
                loadReadyText: '松开刷新',
                loadingText: '正在加载中',
                loadedText: '加载完成',
                showInfo: false,
              ),
              child: ListView.builder(
                itemCount: provider.notification.length,
                itemBuilder: (BuildContext context, index) {
                  return _notificationList(context,
                      GetMessageList.fromJson(provider.notification[index]));
                },
              ),
              onRefresh: () async => provider.dropDownRefreshNotification(),
              onLoad: () async => provider.pullUpLoadingNotification(),
            ),
          );
        } else {
          return Spinkit();
        }
      },
    );
  }

  // 通知列表
  Widget _notificationList(context, index) {
    return Container(
      margin: EdgeInsets.only(
        left: ScreenUtil().setWidth(25.0),
        right: ScreenUtil().setWidth(25.0),
      ),
      child: InkWell(
        child: Card(
          child: Container(
            width: ScreenUtil().setWidth(700.0),
            padding: EdgeInsets.only(
              left: ScreenUtil().setWidth(20.0),
              right: ScreenUtil().setWidth(20.0),
            ),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                SizedBox(
                  height: ScreenUtil().setWidth(30.0),
                ),
                _notificationListTitle(
                    index.img, index.title, index.createTime),
                SizedBox(
                  height: ScreenUtil().setWidth(30.0),
                ),
                _notificationListContext(index.content),
                SizedBox(
                  height: ScreenUtil().setWidth(20.0),
                ),
              ],
            ),
          ),
        ),
        onTap: () => NavigatorUtil.jump(context,
            '/detailsSystemNotification?id=${index.id}&headTitle=${Uri.encodeComponent(index.title)}'),
      ),
    );
  }

  // 通知列表头部
  Widget _notificationListTitle(img, title, createTime) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: <Widget>[
        Row(
          children: <Widget>[
            Image.network(img,
                width: ScreenUtil().setWidth(50),
                height: ScreenUtil().setWidth(50.0)),
            SizedBox(
              width: ScreenUtil().setWidth(10.0),
            ),
            Text(
              title,
              style: TextStyle(
                fontSize: ScreenUtil().setSp(32.0),
                color: Colors.black87,
              ),
            ),
          ],
        ),
        Text(
          createTime,
          style: TextStyle(
            fontSize: ScreenUtil().setSp(28.0),
            color: Colors.black38,
          ),
        ),
      ],
    );
  }

  // 系统消息内容
  Widget _notificationListContext(content) {
    return Container(
      alignment: Alignment.topLeft,
      child: Text(
        content,
        overflow: TextOverflow.ellipsis,
        maxLines: 2,
        textAlign: TextAlign.left,
        style: TextStyle(
          fontSize: ScreenUtil().setSp(28.0),
          color: Colors.black54,
        ),
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
